package com.yorick.authority.service.impl;

import com.yorick.authority.entitys.BaseListDto;
import com.yorick.authority.entitys.IPWhiteListDto;
import com.yorick.authority.modal.request.BaseListRequest;
import com.yorick.authority.modal.request.ipWhiteList.IPWhiteListAddRequest;
import com.yorick.authority.modal.request.ipWhiteList.IPWhiteListDeleteRequest;
import com.yorick.authority.modal.request.ipWhiteList.IPWhiteListDetailRequest;
import com.yorick.authority.modal.request.ipWhiteList.IPWhiteListEditRequest;
import com.yorick.authority.modal.response.BaseListResponse;
import com.yorick.authority.modal.response.ipWhiteList.IPWhiteListDetailResponse;
import com.yorick.authority.service.AbstractAuthorityService;
import com.yorick.authority.service.IPWhiteListService;
import com.yorick.common.Result.SimpleFactory.ResultFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class IPWhiteListServiceImpl extends AbstractAuthorityService implements IPWhiteListService {


    @Override
    public BaseListResponse getIPWhiteList(BaseListRequest request) {
        Integer currentPage = request.getCurrentPage();
        Integer pageSize = request.getPageSize();
        Integer start = (currentPage - 1) * pageSize;
        BaseListDto baseListDto = BaseListDto.builder()
                .start(start)
                .pageSize(pageSize)
                .keyword(request.getKeyword())
                .build();
        List<IPWhiteListDto> list = ipWhiteListMapper.getIPWhiteList(baseListDto);

        BaseListResponse response = BaseListResponse.builder()
                .list(list)
                .total(ipWhiteListMapper.ipWhiteListTotal(baseListDto))
                .build();

        return response;
    }

    @Override
    public Map<String, Object> addIPWhiteList(IPWhiteListAddRequest request) {
        IPWhiteListDto data = IPWhiteListDto.builder()
                .IP(request.getIP())
                .description(request.getDescription())
                .build();
        ipWhiteListMapper.insertIPWhiteList(data);
        //清除mybatis缓存
        securityMapper.clearCache();
        return ResultFactory.success().display();
    }

    @Override
    public Map<String, Object> editIPWhiteList(IPWhiteListEditRequest request) {
        IPWhiteListDto data = IPWhiteListDto.builder()
                .id(request.getId())
                .IP(request.getIP())
                .description(request.getDescription())
                .enabled(request.getEnabled())
                .build();
        ipWhiteListMapper.updateIPWhiteList(data);
        //清除mybatis缓存
        securityMapper.clearCache();
        return ResultFactory.success().display();
    }

    @Override
    public IPWhiteListDetailResponse ipWhiteListDetail(IPWhiteListDetailRequest request) {
        IPWhiteListDto data = ipWhiteListMapper.getIPWhiteListById(request.getId());

        IPWhiteListDetailResponse response = IPWhiteListDetailResponse.builder()
                .id(data.getId())
                .IP(data.getIP())
                .description(data.getDescription())
                .enabled(data.getEnabled())
                .build();
        return response;
    }

    @Override
    public Map<String, Object> deleteIPWhiteList(IPWhiteListDeleteRequest request) {
        ipWhiteListMapper.deleteIPWhiteListById(request.getId());
        //清除mybatis缓存
        securityMapper.clearCache();
        return ResultFactory.success().display();
    }

}
